package traffic_light;

import android.graphics.Bitmap;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import util.opencvUtils;

public class traffic_light {
    public static int traffic_Reconize(Bitmap bitmap) {
        int result, red = 0, green = 0, yellow = 0;
        Mat image = opencvUtils.bitmapToMat(bitmap);
        opencvUtils.toGaussUnClearMat(image, 5, 5, 0);
        Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2HSV);
        Mat mat1 = new Mat(image.rows(), image.cols(), CvType.CV_8UC1, new Scalar(0));
        byte[] data = new byte[mat1.cols()];
        Core.inRange(image, new Scalar(48, 100, 100), new Scalar(78, 255, 255), mat1);//绿色
        for(int i = 0; i < mat1.rows(); i++) {
            mat1.get(i, 0, data);
            for(int j = 0; i < mat1.cols(); j++) {
                if(data[j] == 255){
                    green++;
                }
            }
        }
        Core.inRange(image, new Scalar(0, 100, 100), new Scalar(9, 255, 255), mat1);//红色
        for(int i = 0; i < mat1.rows(); i++) {
            mat1.get(i, 0, data);
            for(int j = 0; i < mat1.cols(); j++) {
                if(data[j] == 255){
                    red++;
                }
            }
        }
        Core.inRange(image, new Scalar(170, 100, 100), new Scalar(180, 255, 255), mat1);//待修改
        for(int i = 0; i < mat1.rows(); i++) {
            mat1.get(i, 0, data);
            for(int j = 0; i < mat1.cols(); j++) {
                if(data[j] == 255){
                    yellow++;
                }
            }
        }
        image.release();
        mat1.release();
        if(green > red) {
            if(green > yellow){
                result = 1;
            } else {
                result = 3;
            }
        } else {
            if(red > yellow) {
                result = 2;
            } else {
                result = 3;
            }
        }
        return result;
    }
}
